﻿Partial Public Class WebForm2

    Inherits System.Web.UI.Page
    Private db As hc_databaseDataContext
    Private str As String



    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        db = New hc_databaseDataContext
        Dim var = From it In db.Kundes _
                  Where it.Name IsNot "" _
        Distinct Select it.Name

        For Each namen As String In var

            dd_kunde.Items.Add(namen)

        Next
        If Not IsPostBack Then
            Dim kundenarry As New ArrayList
            With kundenarry
                .Add("Handel und Logistik")
                .Add("Industrie")
                .Add("Dienstleistung")
                .Add("Luftfahrt")
                .Add("Energie")
                .Add("Kreditwesen")
                .Add("Gesundheitswesen")
                .Add("Bildungswesen")
                .Add("Gastgewerbe")
                dd_branche.DataSource = kundenarry
                dd_branche.DataBind()
            End With
        End If
    End Sub




    Protected Sub btn_weiter_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_weiter.Click
        db = New hc_databaseDataContext
        txt_name.BorderColor = Drawing.Color.Black
        txt_plz.ForeColor = Drawing.Color.Black
        lbl_fehler.Text = ""
        txt_hausnummer.BorderColor = Drawing.Color.Black


        If chk_intern.Checked = False Then
            If chb_neuerKunde.Checked Then

                If txt_name.Text = "" Then
                    txt_name.BorderColor = Drawing.Color.Red
                    Exit Sub
                End If
                Dim var = (From it In db.Kundes _
                          Where it.Name = txt_name.Text _
                          Select it).Count
                If var > 0 Then
                    lbl_fehler.Text = "Es existiert bereits ein Kunde mit diesem Namen"
                    lbl_fehler.BorderColor = Drawing.Color.Red
                    Exit Sub
                End If
                If regex(txt_hausnummer.Text) = False Then


                    txt_hausnummer.BorderColor = Drawing.Color.Red
                    Exit Sub
                End If
                If regex(txt_plz.Text) = False Then

                    txt_plz.BorderColor = Drawing.Color.Red
                    Exit Sub
                End If

                Dim kunde As New Kundes

                kunde.Name = txt_name.Text
                kunde.Ort = txt_ort.Text
                kunde.Postleitzahl = txt_plz.Text
                kunde.Strasse_hausnummer = txt_strasse.Text + " " + txt_hausnummer.Text
                kunde.Hompage = txt_hp.Text
                kunde.Kurzbeschreibung = txt_kurzBeschreibung.Text
                kunde.Land = txt_land.Text

                Dim fehlermeldung As Boolean = False


                kunde.Branche = dd_branche.SelectedItem.Text

                If fehlermeldung = True Then
                    Exit Sub
                End If
                db.Kundes.InsertOnSubmit(kunde)
                db.SubmitChanges()
                Dim strURL As String = "projekt_erstellen_zwei.aspx?Kunde=" + kunde.Name
                Session("Kundenstring") = kunde.Name
                Response.Redirect(strURL)

            Else
                Dim strurl As String = "projekt_erstellen_zwei.aspx?Kunde=" + dd_kunde.SelectedValue
                Session("Kundenstring") = dd_kunde.SelectedValue
                Response.Redirect(strurl)
            End If
            Session("Intern") = 0
        Else

            Session("Intern") = 1
            Try
                Dim var = (From it In db.Kundes _
                          Where it.Name = "Hanseatic Consulting" _
                           Select it).Single
                Dim var2 = (From it In db.Ansprechpartners _
                            Where it.Vorname = "Hanseatic" And _
                            it.Kunde_id = var.ID _
                            Select it).Single



                str = "projekt_erstellen_drei.aspx?Kunde=" + var.ID.ToString + "&Ansprechpartner=" + var2.ID.ToString

            Catch ex As Exception
                Dim kunde1 As New Kundes
                Dim ansprechpartner1 As New Ansprechpartner
                kunde1.Name = "Hanseatic Consulting"
                kunde1.Branche = "Beratung"
                kunde1.Hompage = "www.hanseaticconsulting.de"
                kunde1.Kurzbeschreibung = "Stundentische Unternehmensberatung e.V."
                kunde1.Land = "Deutschland"
                kunde1.Ort = "Hamburg"
                kunde1.Strasse_hausnummer = "Max-Brauer-Allee 60"
                kunde1.Postleitzahl = "22765"
                db.Kundes.InsertOnSubmit(kunde1)
                db.SubmitChanges()
                ansprechpartner1.Vorname = "Hanseatic"
                ansprechpartner1.Nachname = "Consulting"
                ansprechpartner1.Telefonnummer = "+49 40 389 83 35"
                ansprechpartner1.Fax = "+49 40 389 83 35"
                ansprechpartner1.email = "info@hanseaticconsulting.de"
                ansprechpartner1.Kundes = kunde1
                db.Ansprechpartners.InsertOnSubmit(ansprechpartner1)
                db.SubmitChanges()
                str = "projekt_erstellen_drei.aspx?Kunde=" + kunde1.ID.ToString + "&Ansprechpartner=" + ansprechpartner1.ID.ToString

            End Try
            Response.Redirect(str)
        End If


    End Sub

    Protected Sub chb_neuerKunde_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles chb_neuerKunde.CheckedChanged
        If chb_neuerKunde.Checked Then

            dd_kunde.Items.Clear()
            mv_neuerkunde.ActiveViewIndex = 0
        Else
            mv_neuerkunde.ActiveViewIndex = 1

        End If
    End Sub

    Protected Sub LinqDataSource1_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.LinqDataSourceSelectEventArgs) Handles LinqDataSource1.Selecting

    End Sub

    Protected Sub chk_intern_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles chk_intern.CheckedChanged

        If chk_intern.Checked = True Then
            dd_kunde.Visible = False
            mv_neuerkunde.Visible = False
        Else
            dd_kunde.Visible = True
            mv_neuerkunde.Visible = True
        End If
    End Sub

    Private Function regex(ByVal kette As String) As Boolean
        Dim reg1 As New Regex("[0-9]")
        Return reg1.IsMatch(kette)

    End Function

    Protected Sub txt_hausnummer_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles txt_hausnummer.TextChanged

    End Sub

    Protected Sub dd_kunde_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles dd_kunde.SelectedIndexChanged

    End Sub

    Protected Sub dd_branche_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles dd_branche.SelectedIndexChanged

    End Sub

End Class